﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tauren.File.Entity.Global;
using Tauren.File.Entity.Model;
using Tauren.File.Entity.Output;
using Tauren.File.Infrastructure.Globals;

namespace Tauren.File.DLL.FileDLL
{
    public class FileBusiness : Base.BaseDLL, IFileBusiness
    {
        public FileBusiness(IConnectionDLLBase _dLLBase) : base(_dLLBase)
        {
        }
        /// <summary>
        /// 获取版本信息信息--多个
        /// </summary>
        /// </summary>
        /// <param name="model">版本信息实体</param>
        /// <returns></returns>
        public List<FilesVersion> GetVersionsByFileId(long fileid) => this.BusinessConnection.Query<FilesVersion>(sql: "SELECT *  FROM  FilesVersion WHERE fileid=@FileId", param: new FilesVersion() { FileId = fileid }).AsList();
        public double GetSeq()
        {
            Files floder = this.BusinessConnection.QueryFirstOrDefault<Files>("SELECT * FROM Files WHERE havedelete=0  ORDER BY autoid DESC LIMIT 0,1");
            if (floder is null) return 1;
            else return (floder.Sequence == 0 ? 1 : floder.Sequence + 1);
        }
        public List<PageDocumentCenterTable> Pages(QueryModel queryModel, out long Total)
        {

            Total = 0;
            string where = "";
            if (!string.IsNullOrEmpty(queryModel.KeyWord))
            {
                where += $"   AND (t.cnname LIKE '%{queryModel.KeyWord}%'  OR t.enname LIKE '%{queryModel.KeyWord}%' )  ";
            }

            if (queryModel.paramters.Any(o => o.Label.ToLower() == "folderid"))
            {
                var floder = queryModel.paramters.FirstOrDefault(P => P.Label.ToLower() == "folderid");
                where += $" AND t.folderid ={ (string.IsNullOrEmpty(floder.Value + "") ? "0" : floder.Value)}    ";
            }
            var paramtets = queryModel.paramters.Where(o => o.Label.ToLower() != "folderid").ToList();
            paramtets.ForEach(p =>
            {
                where += $"  and {p.Label} like '%{p.Value}%'  ";
            });
            if (string.IsNullOrEmpty(queryModel.SortField))
            {
                queryModel.SortField = "sequence";
                queryModel.IsAsc = false;
            }
            else
            {
                queryModel.SortField = queryModel.SortField.ToLower().Replace("dic_", "").Replace("dic", "").Trim();
            }
            if (!queryModel.UseDisabledData)
            {
                where += $" AND enable=1  ";
            }
            string asc = queryModel.IsAsc ? "ASC" : "DESC";
            queryModel.Page = queryModel.Page == 0 ? 1 : queryModel.Page;
            StringBuilder sql = new StringBuilder();
            sql.Append(@$"SELECT count(t.autoid) from files t  WHERE t.havedelete!=1  {where} ;");
            sql.Append(@$" SELECT  autoId,
		                                cnname ,
		                                enname,
		                                ext,
		                                filetype,
		                                '' OrgType,
		                                size,
		                                currentVersion,
		                                `sequence` ,
		                                `path` ,
		                                0 orgid ,
		                                fileuri,
		                                havedelete,creatdate,creator,enable FROM files t WHERE havedelete!=1
                        {where}  ORDER BY t.{queryModel.SortField} {asc} LIMIT {(queryModel.Page - 1) * queryModel.PageSize},{ queryModel.PageSize} ;");
            using (var reader = this.BusinessConnection.QueryMultiple(sql.ToString()))
            {
                Total = reader.ReadFirstOrDefault<int>();
                List<PageDocumentCenterTable> pageModules = reader.Read<PageDocumentCenterTable>().ToList();
                long RowNo = ((queryModel.Page == 0 ? 1 : queryModel.Page) - 1) * queryModel.PageSize + 1;
                pageModules.ForEach(o =>
                {
                    o.RowNo = RowNo;
                    RowNo++;
                });
                return pageModules;
            }
        }
        /// <summary>
        /// 根据文件ID获取数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public List<Files> GetFiles(string[] ids)
        {
            return this.BusinessConnection.Query<Files>($"SELECT * FROM Files WHERE havedelete=0 AND AutoId in({string.Join(",", ids)}) ORDER BY autoid DESC ").AsList();
        }

        public ExPhotoInfo SingePhoto(int empid) => this.BusinessConnection.QueryFirstOrDefault<ExPhotoInfo>(sql: "SELECT p.*,f.fileuri FROM photoinfo p LEFT JOIN files f  ON p.fileid =f.autoid WHERE p.empid=@EmpId", param: new ExPhotoInfo() { EmpId = empid });
    }
}
